<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewAccounts extends JView
{
	function display($tpl=null) {

		global $mainframe, $option;

		//Initialise variables
		$user			= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$db			= & JFactory::getDBO();
		$params 	= & $mainframe->getParams('com_ownservice');

		if($user->gid < 25) {
			if (!Ownservice::checkAccess( 'account', 'manage' )) {
				$mainframe->redirect( 'index.php', JText::_('ALERTNOTAUTH') );
			}
		}

		JHTML::_('behavior.mootools');
		JHTML::_('behavior.modal', 'a.modal');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/icon.css');
		
		//add js autocomplete
		$document->addScript('components/com_ownservice/assets/js/jquery-1.7.2.min.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.bgiframe.min.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.ajaxQueue.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.autocomplete.min.js');
		//add css autocomplete
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-autocomplete/jquery.autocomplete.css');

		//ui
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-ui-1.9.1/themes/base/jquery.ui.all.css');
		//$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/jquery-1.8.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/external/jquery.bgiframe-2.1.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.core.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.widget.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.mouse.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.draggable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.position.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.resizable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.dialog.js');

		if($this->getLayout() == 'accounts') {
			$this->_displayAccounts($tpl);
			return;
		}

		$document->addScriptDeclaration(
			'
				function selectUser(cid, membername)
				{
					document.adminForm.membername.value=membername;
					document.adminForm.user_id.value=cid;
					document.getElementById(\'sbox-window\').close();
				}
				
				jQuery(document).ready(function () {
					jQuery("#membername").autocomplete("index.php?option=com_ownservice&controller=account&task=viewmembers&format=raw&'.JUtility::getToken().'=1", {
					width: 260,
					max: 100,
					matchContains: false,
					mustMatch: false,
					selectFirst: false,
					dataType: "json",
					parse: function(data) {
						return jQuery.map(data, function(row) {
							return {
								data: row,
								value: row.name,
								result: row.name
							};
						});
					},
					formatItem: function(row) {
						return row.name;
					}
					}).result(function(e, row) {
						jQuery("#user_id").val(row.id);
					})
				});
			');

		parent::display($tpl);
	}

	function _displayAccounts($tpl)
	{
		global $mainframe, $option;

		//Initialise variables
		$user			= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$db			= & JFactory::getDBO();
		$params 	= & $mainframe->getParams('com_ownservice');

		if($user->gid < 25) {
			if (!Ownservice::checkAccess( 'account', 'manage' )) {
				$mainframe->redirect( 'index.php', JText::_('ALERTNOTAUTH') );
			}
		}

		//get filter
		$filter_order			= $mainframe->getUserStateFromRequest($option.'.account.filter_order', 'filter_order', 'a.account_no', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.account.filter_order_Dir', 'filter_order_Dir', '', 'cmd');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/icon.css');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$rows			= & $this->get('Data');
		$level			= $ownuser->get('level_id');
		$pageNav	= & $this->get('Pagination');

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canView		= ($user->gid < 25)? Ownservice::checkAccess('account','view','account','all'):1;
		$access->canEdit			= ($user->gid < 25)? Ownservice::checkAccess('account','edit','account','all'):1;
		$access->canAdd			= ($user->gid < 25)? Ownservice::checkAccess('account','add','account','all'):1;
		$access->canDelete		= ($user->gid < 25)? Ownservice::checkAccess('account','delete','account','all'):1;

		$lists = array();

		$lists['order']		= $filter_order;
		$lists['order_Dir']	= $filter_order_Dir;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_RECEIVER_ACCOUNTS'));
		$document->setTitle( $params->get('page_title') );

		//assign data to document
		$this->assignRef('rows', $rows);
		$this->assignRef('pageNav', $pageNav);
		$this->assignRef('access', $access);
		$this->assignRef('lists', $lists);
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);

		parent::display($tpl);
	}
}